package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import java.math.BigDecimal;

import static com.shanxia.codec.CodeConstant.PERCENT;
import static com.shanxia.codec.Decoder.getInt32;
import static com.shanxia.codec.Decoder.getUint8;

/**
 * C -> S
 * 充电桩回复 24 时电费计价策略信息
 */
@Data
public class Cmd1108 extends ProtocolDataBase {

    public static final int MIN_SIZE = 144;

    private int startHour1;                             // 开始小时1
    private int startMinute1;                           // 开始分钟1
    private int endHour1;                               // 结束小时1
    private int endMinute1;                             // 结束分钟1
    private double chargeRate1;                         // 充电费率1
    private double serviceRate1;                        // 服务费率1
    private int startHour2;
    private int startMinute2;
    private int endHour2;
    private int endMinute2;
    private double chargeRate2;
    private double serviceRate2;
    private int startHour3;
    private int startMinute3;
    private int endHour3;
    private int endMinute3;
    private double chargeRate3;
    private double serviceRate3;
    private int startHour4;
    private int startMinute4;
    private int endHour4;
    private int endMinute4;
    private double chargeRate4;
    private double serviceRate4;
    private int startHour5;
    private int startMinute5;
    private int endHour5;
    private int endMinute5;
    private double chargeRate5;
    private double serviceRate5;
    private int startHour6;
    private int startMinute6;
    private int endHour6;
    private int endMinute6;
    private double chargeRate6;
    private double serviceRate6;
    private int startHour7;
    private int startMinute7;
    private int endHour7;
    private int endMinute7;
    private double chargeRate7;
    private double serviceRate7;
    private int startHour8;
    private int startMinute8;
    private int endHour8;
    private int endMinute8;
    private double chargeRate8;
    private double serviceRate8;
    private int startHour9;
    private int startMinute9;
    private int endHour9;
    private int endMinute9;
    private double chargeRate9;
    private double serviceRate9;
    private int startHour10;
    private int startMinute10;
    private int endHour10;
    private int endMinute10;
    private double chargeRate10;
    private double serviceRate10;
    private int startHour11;
    private int startMinute11;
    private int endHour11;
    private int endMinute11;
    private double chargeRate11;
    private double serviceRate11;
    private int startHour12;
    private int startMinute12;
    private int endHour12;
    private int endMinute12;
    private double chargeRate12;
    private double serviceRate12;

    public Cmd1108(){
        super.setCmd(1108);
    }

    public static Cmd1108 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd1108 cmd = new Cmd1108();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        // 1字节开始小时
        cmd.startHour1 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute1 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour1 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute1 = getUint8(dataField[offset++]);
        // 4字节充电费率1
        cmd.chargeRate1 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率1
        cmd.serviceRate1 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour2 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute2 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour2 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute2 = getUint8(dataField[offset++]);
        // 4字节充电费率2
        cmd.chargeRate2 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率2
        cmd.serviceRate2 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour3 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute3 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour3 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute3 = getUint8(dataField[offset++]);
        // 4字节充电费率3
        cmd.chargeRate3 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率3
        cmd.serviceRate3 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour4 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute4 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour4 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute4 = getUint8(dataField[offset++]);
        // 4字节充电费率4
        cmd.chargeRate4 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率4
        cmd.serviceRate4 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour5 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute5 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour5 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute5 = getUint8(dataField[offset++]);
        // 4字节充电费率5
        cmd.chargeRate5 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率5
        cmd.serviceRate5 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour6 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute6 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour6 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute6 = getUint8(dataField[offset++]);
        // 4字节充电费率6
        cmd.chargeRate6 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率6
        cmd.serviceRate6 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour7 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute7 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour7 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute7 = getUint8(dataField[offset++]);
        // 4字节充电费率7
        cmd.chargeRate7 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率7
        cmd.serviceRate7 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour8 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute8 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour8 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute8 = getUint8(dataField[offset++]);
        // 4字节充电费率8
        cmd.chargeRate8 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率8
        cmd.serviceRate8 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour9 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute9 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour9 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute9 = getUint8(dataField[offset++]);
        // 4字节充电费率9
        cmd.chargeRate9 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率9
        cmd.serviceRate9 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour10 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute10 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour10 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute10 = getUint8(dataField[offset++]);
        // 4字节充电费率10
        cmd.chargeRate10 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率10
        cmd.serviceRate10 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour11 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute11 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour11 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute11 = getUint8(dataField[offset++]);
        // 4字节充电费率12
        cmd.chargeRate12 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率12
        cmd.serviceRate12 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 1字节开始小时
        cmd.startHour12 = getUint8(dataField[offset++]);
        // 1字节开始分钟
        cmd.startMinute12 = getUint8(dataField[offset++]);
        // 1字节结束小时
        cmd.endHour12 = getUint8(dataField[offset++]);
        // 1字节结束分钟
        cmd.endMinute12 = getUint8(dataField[offset++]);
        // 4字节充电费率12
        cmd.chargeRate12 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        offset += 4;
        // 4字节服务费率12
        cmd.serviceRate12 = new BigDecimal(getInt32(dataField, offset)).multiply(PERCENT).doubleValue();
        return cmd;
    }

    @Override
    public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append("#cmd1108#").append("\n");
        buffer.append("开始小时1:").append(startHour1).append("\n");
        buffer.append("开始分钟1:").append(startMinute1).append("\n");
        buffer.append("结束小时1:").append(endHour1).append("\n");
        buffer.append("结束分钟1:").append(endMinute1).append("\n");
        buffer.append("充电费率1:").append(chargeRate1).append("\n");
        buffer.append("服务费率1:").append(serviceRate1).append("\n");

        buffer.append("开始小时2:").append(startHour2).append("\n");
        buffer.append("开始分钟2:").append(startMinute2).append("\n");
        buffer.append("结束小时2:").append(endHour2).append("\n");
        buffer.append("结束分钟2:").append(endMinute2).append("\n");
        buffer.append("充电费率2:").append(chargeRate2).append("\n");
        buffer.append("服务费率2:").append(serviceRate2).append("\n");

        buffer.append("开始小时3:").append(startHour3).append("\n");
        buffer.append("开始分钟3:").append(startMinute3).append("\n");
        buffer.append("结束小时3:").append(endHour3).append("\n");
        buffer.append("结束分钟3:").append(endMinute3).append("\n");
        buffer.append("充电费率3:").append(chargeRate3).append("\n");
        buffer.append("服务费率3:").append(serviceRate3).append("\n");

        buffer.append("开始小时4:").append(startHour4).append("\n");
        buffer.append("开始分钟4:").append(startMinute4).append("\n");
        buffer.append("结束小时4:").append(endHour4).append("\n");
        buffer.append("结束分钟4:").append(endMinute4).append("\n");
        buffer.append("充电费率4:").append(chargeRate4).append("\n");
        buffer.append("服务费率4:").append(serviceRate4).append("\n");

        buffer.append("开始小时5:").append(startHour5).append("\n");
        buffer.append("开始分钟5:").append(startMinute5).append("\n");
        buffer.append("结束小时5:").append(endHour5).append("\n");
        buffer.append("结束分钟5:").append(endMinute5).append("\n");
        buffer.append("充电费率5:").append(chargeRate5).append("\n");
        buffer.append("服务费率5:").append(serviceRate5).append("\n");

        buffer.append("开始小时6:").append(startHour6).append("\n");
        buffer.append("开始分钟6:").append(startMinute6).append("\n");
        buffer.append("结束小时6:").append(endHour6).append("\n");
        buffer.append("结束分钟6:").append(endMinute6).append("\n");
        buffer.append("充电费率6:").append(chargeRate6).append("\n");
        buffer.append("服务费率6:").append(serviceRate6).append("\n");

        buffer.append("开始小时7:").append(startHour7).append("\n");
        buffer.append("开始分钟7:").append(startMinute7).append("\n");
        buffer.append("结束小时7:").append(endHour7).append("\n");
        buffer.append("结束分钟7:").append(endMinute7).append("\n");
        buffer.append("充电费率7:").append(chargeRate7).append("\n");
        buffer.append("服务费率7:").append(serviceRate7).append("\n");

        buffer.append("开始小时8:").append(startHour8).append("\n");
        buffer.append("开始分钟8:").append(startMinute8).append("\n");
        buffer.append("结束小时8:").append(endHour8).append("\n");
        buffer.append("结束分钟8:").append(endMinute8).append("\n");
        buffer.append("充电费率8:").append(chargeRate8).append("\n");
        buffer.append("服务费率8:").append(serviceRate8).append("\n");

        buffer.append("开始小时9:").append(startHour9).append("\n");
        buffer.append("开始分钟9:").append(startMinute9).append("\n");
        buffer.append("结束小时9:").append(endHour9).append("\n");
        buffer.append("结束分钟9:").append(endMinute9).append("\n");
        buffer.append("充电费率9:").append(chargeRate9).append("\n");
        buffer.append("服务费率9:").append(serviceRate9).append("\n");

        buffer.append("开始小时10:").append(startHour10).append("\n");
        buffer.append("开始分钟10:").append(startMinute10).append("\n");
        buffer.append("结束小时10:").append(endHour10).append("\n");
        buffer.append("结束分钟10:").append(endMinute10).append("\n");
        buffer.append("充电费率10:").append(chargeRate10).append("\n");
        buffer.append("服务费率10:").append(serviceRate10).append("\n");

        buffer.append("开始小时11:").append(startHour11).append("\n");
        buffer.append("开始分钟11:").append(startMinute11).append("\n");
        buffer.append("结束小时11:").append(endHour11).append("\n");
        buffer.append("结束分钟11:").append(endMinute11).append("\n");
        buffer.append("充电费率11:").append(chargeRate11).append("\n");
        buffer.append("服务费率11:").append(serviceRate11).append("\n");

        buffer.append("开始小时12:").append(startHour12).append("\n");
        buffer.append("开始分钟12:").append(startMinute12).append("\n");
        buffer.append("结束小时12:").append(endHour12).append("\n");
        buffer.append("结束分钟12:").append(endMinute12).append("\n");
        buffer.append("充电费率12:").append(chargeRate12).append("\n");
        buffer.append("服务费率12:").append(serviceRate12).append("\n");

        return buffer.toString();
    }
}
